<?php
include "../Service/majorsService.php";

if(isset($_GET["action"]))
    $action = $_GET["action"];
else
    $action = "show";
if($action == "show")
{
    $result = getMajors();
    $n = 0;
    while($row = mysql_fetch_array($result))
    {
        $major['id'] = $row["MAJOR_ID"];
        $major['title'] = $row["MAJOR_TITLE"];
        $major['descript'] = $row["MAJOR_DESCRIPTION"];
        $arrMajor[] = $major;
        $n++;
    }
}
else if($action == "added")
{
    $major['title'] = $_POST["title"];
    $major['descript'] = $_POST["descript"];
    $result = insertMajors($major);
    echo "<script>window.location.href='../admin/mod.php?page=majors'</script>";
}
else if($action == "deleted")
{
    $id = $_GET['id'];
    $result = deleteMajor($id);
    if(!$result)
        echo "<script>alert('Khong the thuc hien')</script>";    
    echo "<script>window.location.href='../admin/mod.php?page=majors'</script>";
}
else if ($action == "edit")
{
    $id = $_GET["id"];
    $result = getMajorsById($id);
    $major['id'] = $result["MAJOR_ID"];
    $major['title'] = $result["MAJOR_TITLE"];
    $major['descript'] = $result["MAJOR_DESCRIPTION"];
}
else if($action == "edited"){
    $major['id'] = $_GET["id"];
    $major['title'] = $_POST["title"];
    $major['descript'] = $_POST["descript"];
    $result = editMajor($major);
    if(!$result)
        echo "<script>alert('Khong the thuc hien')</script>";    
    echo "<script>window.location.href='../admin/mod.php?page=majors'</script>";
}
function showMajor(){
    $result = getMajors();
    $n = 0;
    while($row = mysql_fetch_array($result))
    {
        $major['id'] = $row["MAJOR_ID"];
        $major['title'] = $row["MAJOR_TITLE"];
        $major['descript'] = $row["MAJOR_DESCRIPTION"];
        $arrMajor[] = $major;
        $n++;
    }
    return $arrMajor;
}

?>